package com.yunqiao.api.task;

import com.yunqiao.service.api.OrderService;
import com.yunqiao.service.api.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;


/**
 * 任务监听器 监听任务是否完成，如果完成 自动改变状态
 * @author Administrator
 *
 */
@Component
@Configuration
public class TaskListener implements SchedulingConfigurer {

    @Autowired
    private UserService userService;

	/**
	 * 将订单冻结金额转到实时账户
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void dealOrderMoney() {
        userService.dealOrderMoney();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(setTaskExecutors());
    }

    /**
     * 开辟3个线程来处理任务
     */
    @Bean
    public Executor setTaskExecutors(){
        // 3个线程来处理。
        return new ScheduledThreadPoolExecutor(3);
    }

}